package com.information.manage.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.information.manage.basic.common.result.Result;
import com.information.manage.basic.mapper.SysRelUserFileMapper;
import com.information.manage.basic.mapper.SysUserMapper;
import com.information.manage.basic.model.dto.CollectionDTO;
import com.information.manage.basic.model.entity.SysRelUserFileDO;
import com.information.manage.basic.model.entity.SysUserDO;
import com.information.manage.basic.model.query.CollectionPageQuery;
import com.information.manage.basic.model.vo.SysRelUserFileVO;
import com.information.manage.dao.AnalysisFileMapper;
import com.information.manage.model.entity.AnalysisFileDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 92110
 */
@RestController
@RequestMapping("/collection")
@Api(tags = "用户收藏")
public class RelUserFileController {

    @Resource
    private SysRelUserFileMapper sysRelUserFileMapper;
    @Resource
    private AnalysisFileMapper analysisFileMapper;
    @Resource
    private SysUserMapper sysUserMapper;


    @GetMapping("/list/page")
    @ApiOperation(value = "收藏列表", notes = "收藏列表")
    public Result<SysRelUserFileVO> collectionList(CollectionPageQuery collectionPageQuery) {
        Page<SysRelUserFileDO> page = new Page<>(collectionPageQuery.getPageNum(), collectionPageQuery.getPageSize());
        Page<SysRelUserFileDO> list = sysRelUserFileMapper
                .selectPage(page, new QueryWrapper<SysRelUserFileDO>()
                        .eq("user_id", collectionPageQuery.getUserId()));
        SysUserDO user = sysUserMapper.selectById(collectionPageQuery.getUserId());
        List<AnalysisFileDo> items = new ArrayList<>();
        if (!list.getRecords().isEmpty()) {
            list.getRecords().forEach(item -> items.add(analysisFileMapper.selectById(item.getFileId())));
        }

        return Result.ok(new SysRelUserFileVO(user, items));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加收藏", notes = "添加收藏")
    public Result<String> addCollection(@RequestBody CollectionDTO collectionDTO) {
        try {
            SysRelUserFileDO sysRelUserFileDO = new SysRelUserFileDO();
            sysRelUserFileDO.setFileId(collectionDTO.getFileId());
            sysRelUserFileDO.setUserId(collectionDTO.getUserId());
            sysRelUserFileMapper.insert(sysRelUserFileDO);
            return Result.ok("添加成功");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除收藏", notes = "删除收藏")
    public Result<String> deleteCollection(@RequestBody CollectionDTO collectionDTO) {
        try {
            sysRelUserFileMapper.delete(new QueryWrapper<SysRelUserFileDO>()
                   .eq("file_id", collectionDTO.getFileId())
                   .eq("user_id", collectionDTO.getUserId()));
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}
